package com.bio.jsf;

import com.bio.jpa.entities.UserHasGroup;
import com.bio.jsf.util.JsfUtil;
import com.bio.jpa.session.UserHasGroupFacade;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import com.bio.jpa.entities.UserHasGroupPK;

@ManagedBean(name = "userHasGroupController")
@SessionScoped
public class UserHasGroupController implements Serializable {

    private UserHasGroup current;
    private List<UserHasGroup> userItems;
    @EJB
    private UserHasGroupFacade ejbFacade;

    public List<UserHasGroup> getUserItems() {
        userItems = getFacade().findAll();
        return userItems;
    }

    public void setUserItems(List<UserHasGroup> userItems) {
        this.userItems = userItems;
    }

    public UserHasGroupController() {
        current = new UserHasGroup(new UserHasGroupPK());
    }

    public UserHasGroup getCurrent() {
        return current;
    }

    public void setCurrent(UserHasGroup selected) {
        this.current = selected;
    }

    private UserHasGroupFacade getFacade() {
        return ejbFacade;
    }

    public String prepareList() {
        current = null;
        return "List";
    }

    public String prepareView() {
        return "View";
    }

    public String prepareCreate() {
        current = new UserHasGroup();
        current.setUserHasGroupPK(new com.bio.jpa.entities.UserHasGroupPK());
        return "Create";
    }

    public String create() {
        try {
            current.setUserHasGroupPK(new UserHasGroupPK(current.getUserHasGroupPK().getUidFk(), current.getUserHasGroupPK().getGidFk()));
            current.setLastUpdate(new Date());
            getFacade().create(current);
            current = null;
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserHasGroupCreated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            e.printStackTrace();
            return null;
        }
    }

    public String prepareEdit() {
        return "Edit";
    }

    public String update() {
        try {
            current = getFacade().edit(current);
            userItems.size();
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserHasGroupUpdated"));
            return "View";
        } catch (Exception e) {
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            return null;
        }
    }

    public String destroy() {
        performDestroy();
        current = null;
        return "List";
    }

    public String justDestroy() {
        performDestroy();
        current = null;
        return "View";
    }

    public String destroyAndView() {
        performDestroy();
        current = null;

        return "List";

    }

    private void resetLists() {
        userItems = null;

    }

    private void performDestroy() {
        try {
            System.out.println("Removing " + current);
            getFacade().remove(current);
            getUserItems().remove(current);
            current = null;
            JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("UserHasGroupDeleted"));
        } catch (Exception e) {
            e.printStackTrace();
            JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
        }
    }

    @FacesConverter(forClass = UserHasGroup.class)
    public static class UserHasGroupControllerConverter implements Converter {

        private static final String SEPARATOR = "#";
        private static final String SEPARATOR_ESCAPED = "\\#";

        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            UserHasGroupController controller = (UserHasGroupController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "userHasGroupController");
            return controller.ejbFacade.find(getKey(value));
        }

        com.bio.jpa.entities.UserHasGroupPK getKey(String value) {
            com.bio.jpa.entities.UserHasGroupPK key;
            String values[] = value.split(SEPARATOR_ESCAPED);
            key = new com.bio.jpa.entities.UserHasGroupPK();
            key.setUidFk(Integer.parseInt(values[0]));
            key.setGidFk(Integer.parseInt(values[1]));
            return key;
        }

        String getStringKey(com.bio.jpa.entities.UserHasGroupPK value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value.getUidFk());
            sb.append(SEPARATOR);
            sb.append(value.getGidFk());
            return sb.toString();
        }

        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof UserHasGroup) {
                UserHasGroup o = (UserHasGroup) object;
                return getStringKey(o.getUserHasGroupPK());
            } else {
                throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + UserHasGroupController.class.getName());
            }
        }
    }
}
